package com.hll.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hll.api.entity.ScoketMsg;
import com.hll.api.exception.MyException;
import com.hll.api.service.TulingService;
import com.hll.api.util.HttpRequestUtil;
import com.hll.api.util.HttpUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 图灵机器人
 * @author 何龙
 * @create 2019-07-14 14:36
 */
@Service("TulingServiceImpl")
public class TulingServiceImpl implements TulingService {
	private static final String API="https://api.jisuapi.com/iqa/query?appkey=ef74aeddad0f4e0b&question=";
	private static final String URL="http://openapi.tuling123.com/openapi/api/v2";
	private static final String PREFIX ="{\"reqType\":0,\"perception\":{\"inputText\":{\"text\":\"";
	private static final String SUFFIX ="\"}},\"userInfo\":{\"apiKey\":\"25d969b6e84149018ae170b0f99a2dcc\",\"userId\":\"174bb06e1d1b9989\"}}";

	@Override
	public JSONObject sendText(String content) {
		Assert.notNull(content,"发送内容不能为空");
		String s = HttpRequestUtil.get(API + content);
		JSONObject object = JSON.parseObject(s);
		if (object.getIntValue("status")==0){
			return object.getJSONObject("result");
		}else {
			throw new MyException("接口调用失败");
		}
	}

	@Override
	public ScoketMsg send(String content) {
		String s = HttpUtils.postJson(URL, PREFIX + content + SUFFIX);
		JSONObject object = JSON.parseObject(s);
		JSONArray results = object.getJSONArray("results");
		return ScoketMsg.success(results);
	}
}
